package controllers.product;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lib_model.Orther.ReturnObj;
import lib_model.Product.Tax_product;

/**
 * Servlet implementation class AddTax
 */
@WebServlet("/product/AddTax")
public class AddTax extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AddTax() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		try {
			RequestDispatcher dis = request
					.getRequestDispatcher("../administrator/tax.jsp");
			float value = (Float.parseFloat(request.getParameter("txtValue")));
			int type = (Integer.parseInt(request.getParameter("txtType")));
			if (request.getParameter("txtValue") != null) {
				Tax_product t = new Tax_product();
				t.type = type;
				t.value = value;
				ReturnObj obj = t.Add();
				if (obj.Result == true) {
					System.out.println(obj.Result);
					request.setAttribute("Mes", "Suscessfull !");
				} else {
					System.out.println(obj.Result);
					request.setAttribute("Mes",
							"Error ! Can't create Tax product");
				}
			} else {
				request.setAttribute("Mes", "Error ! Can't create Tax product");
			}
			
			
			dis.forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
			RequestDispatcher dis = request
					.getRequestDispatcher("../administrator/tax.jsp");
			request.setAttribute("Mes", "Error ! Can't create Tax product");
			dis.forward(request, response);

		}
	}
}
